package com.fadadianqi.logistics.admin.staff.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.logistics.admin.staff.model.Group;
import com.fadadianqi.logistics.admin.staff.model.Master;
import com.fadadianqi.logistics.admin.staff.service.GroupService;
import com.fadadianqi.logistics.admin.staff.service.MasterGroupService;
import com.fadadianqi.logistics.admin.staff.service.MasterService;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.commons.ParamHelper;
import com.yeqiangwei.commons.util.BeanUtils;
import com.yeqiangwei.commons.util.ParamUtils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


/**
 * @author aroot
 */
@Controller
public class MasterController {

    @RequestMapping(value = "/a/staff/master/list", method = RequestMethod.GET)
    public String showList(HttpServletRequest request, ModelMap modelMap) {
        List<Master> _masters = masterService.findAll();
        List<MasterVO> masters = new ArrayList<MasterController.MasterVO>();
        if (ValidatorHelper.isNotEmpty(_masters)) {
            for (Master m : _masters) {
                MasterVO vo = new MasterVO();
                BeanUtils.copyProperties(vo, m);
                vo.setGroups(masterGroupService.findGroupsByMaster(m));
                masters.add(vo);
            }
        }
        modelMap.put("masters", masters);
        return "/admin/staff/masterList";
    }

    @RequestMapping(value = "/a/staff/master/form", method = RequestMethod.GET)
    public String showForm(HttpServletRequest request, ModelMap modelMap) {
        String id = ParamHelper.getId(request, null);

        Master m = masterService.getById(id);
        modelMap.put("master", m);

        List<Group> mGroups = masterGroupService.findGroupsByMaster(m);
        modelMap.put("mGroups", mGroups);

        List<Group> groups = groupService.findAll();
        modelMap.put("groups", groups);

        return "/admin/staff/masterForm";
    }

    @RequestMapping(value = "/a/ajax/master/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> save(HttpServletRequest request, ModelMap modelMap) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        AjaxResult rs = null;
        String id = ParamHelper.getId(request, null);
        String name = ParamHelper.getName(request, null);
        String password = ParamHelper.getPassword(request, null);
        Master m = new Master();
        m.setId(id);
        m.setName(name);
        m.setPassword(password);
        try {
            masterService.save(m);
            //
            String _gids = ParamUtils.getString(request, "gids", null);
            if (ValidatorHelper.isNotEmpty(_gids)) {
                masterGroupService.delByMasterId(m.getId());
                String[] gids = _gids.split(",");
                for (String gid : gids) {
                    Group g = groupService.getById(gid);
                    masterGroupService.masterToGroup(m, g);
                }
            }
            rs = new AjaxResult(m, Boolean.TRUE, null);
        } catch (Exception e) {
            String msg = messageSource.getMessage(e.getMessage(), null, Locale.getDefault());
            rs = new AjaxResult(m, Boolean.FALSE, msg);
        }
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }


    @RequestMapping(value = "/a/ajax/dologin", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> dologin(HttpServletRequest request
            , HttpServletResponse response, ModelMap modelMap) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        AjaxResult rs = null;
        String name = ParamHelper.getName(request, null);
        String password = ParamHelper.getPassword(request, null);
        Master m = masterService.getByName(name);
        if (m != null && m.getPassword().equalsIgnoreCase(password)) {
            MasterHelper.save(request, response, m);
            rs = new AjaxResult(m, Boolean.TRUE, null);
        } else {
            rs = new AjaxResult(m, Boolean.FALSE, null);
        }
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }

    @RequestMapping(value = "/a/login", method = RequestMethod.GET)
    public String loginForm(HttpServletRequest request, ModelMap modelMap) {
        String backURL = ParamUtils.getString(request, "backURL", "");
        modelMap.put("backURL", backURL);
        return "/admin/staff/login";
    }

    @RequestMapping(value = "/a/logout", method = RequestMethod.GET)
    public String logout(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        MasterHelper.del(request, response);
        return "redirect:/";
    }

    @Autowired
    private MasterService masterService;

    @Autowired
    private MasterGroupService masterGroupService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private MessageSource messageSource;


    public static class MasterVO extends Master {

        /**
         *
         */
        private static final long serialVersionUID = 5950090283065930238L;

        private List<Group> groups;

        public List<Group> getGroups() {
            return groups;
        }

        public void setGroups(List<Group> groups) {
            this.groups = groups;
        }
    }
}
